/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.logging.Level;

import util.LoggingUtils;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class HttpRequestFacade {

	public static void postUpdateComplete() {
		final String classMethod = "HttpRequestFacade .postUpdateComplete()";
		LoggingUtils.logStart(classMethod);

		try {
			HttpPostConnector c = new HttpPostConnector(new URL(model.Settings.UPDATE_CHECKER_URL),
					HttpPostConnector.getBoxToken() + "&reqtype=completeupdate");
			c.post();
		} catch (MalformedURLException ex) {
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
	}
	
	public static void postContentUpdateConfirmation(String jsonparameter) {
		final String classMethod = "HttpRequestFacade .postContentUpdateConfirmation()";
		LoggingUtils.logStart(classMethod);

		//System.out.println(model.Settings.UPDATE_CHECKER_URL+HttpPostConnector.getBoxToken() + "&reqtype=ContentUpdateConfirmation&jsonparameter="+jsonparameter);
		
		try {
			HttpPostConnector c = new HttpPostConnector(new URL(model.Settings.UPDATE_CHECKER_URL),
					HttpPostConnector.getBoxToken() + "&reqtype=ContentUpdateConfirmation&jsonparameter="+jsonparameter);
			c.post();
		} catch (MalformedURLException ex) {
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
	}

	public static void postPlayerException(String level, String message,String LogTitle) {
		final String classMethod = "HttpRequestFacade .postPlayerException()"; 
		LoggingUtils.logStart(classMethod);

		try {
			HttpPostConnector c = new HttpPostConnector(new URL(model.Settings.UPDATE_CHECKER_URL),
					HttpPostConnector.getBoxToken() + "&reqtype=playerException&message="+message+"&level="+level+"&LogTitle="+LogTitle);
			c.post();
		} catch (MalformedURLException ex) {
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
	}

	public static void PostToConvertImageProductionRequest(String ImageName,String message)
	{
		final String classMethod = "HttpRequestFacade .PostToConvertImage()";
		LoggingUtils.logStart(classMethod);

		String url = model.Settings.UPDATE_CHECKER_URL+"?"+HttpPostConnector.getBoxToken() + "&message="+message+"&ImageName="+ImageName;
		System.out.println(url);
		
		try {
			HttpPostConnector c = new HttpPostConnector(new URL(model.Settings.UPDATE_CHECKER_URL),
					HttpPostConnector.getBoxToken() + "&reqtype=productionRequestForImage&message="+message+"&ImageName="+ImageName);
			c.post();
		} catch (MalformedURLException ex) {
			ex.printStackTrace();
			LoggingUtils.log(Level.SEVERE, "Error in "+classMethod,ex);
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
	}

	public static void resendContent() {
		final String classMethod = "HttpRequestFacade .resendContent()";
		LoggingUtils.logStart(classMethod);
		try {
			System.out.println(model.Settings.UPDATE_CHECKER_URL+HttpPostConnector.getBoxToken() + "&reqtype=resendcontent");
			
			HttpPostConnector c = new HttpPostConnector(new URL(model.Settings.UPDATE_CHECKER_URL),
					HttpPostConnector.getBoxToken() + "&reqtype=resendcontent");
			c.post();
		} catch (MalformedURLException ex) {
			LoggingUtils.log(Level.SEVERE, "Error in resendContent "+classMethod,ex);
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
	}

	public static void PostToConvertImage(int imageID) {
		final String classMethod = "HttpRequestFacade .sendInvalidImageNotification()";
		LoggingUtils.logStart(classMethod);
		
		try {
			System.out.println(HttpPostConnector.getBoxToken() + "&reqtype=convertimage&image_id=" + imageID);
			
			HttpPostConnector c = new HttpPostConnector(new URL(model.Settings.UPDATE_CHECKER_URL),
					HttpPostConnector.getBoxToken() + "&reqtype=convertimage&image_id=" + imageID);
			c.post();
		} catch (MalformedURLException ex) {
			LoggingUtils.log(Level.SEVERE, "Error in sendInvalidImageNotification "+classMethod,ex);
			ex.printStackTrace();
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
	}

	public static URL getWebtoolURL() {
		final String classMethod = "HttpRequestFacade .getWebtoolURL()";
		LoggingUtils.logStart(classMethod);

		try {
			return new URL(model.Settings.UPDATE_CHECKER_URL);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			LoggingUtils.log(Level.SEVERE, "Error in getWebtoolURL "+classMethod,e);
			e.printStackTrace();
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
		return null;
	}

	public static void sendToPlayerUpdatePage(Map<String, String> data) {
		final String classMethod = "HttpRequestFacade .sendToPlayerUpdatePage()";
		LoggingUtils.logStart(classMethod);

		StringBuilder sb = new StringBuilder();
		for (String key : data.keySet()) {
			try {
				sb.append("&" + key + "=" + URLEncoder.encode(data.get(key), "UTF8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				LoggingUtils.log(Level.SEVERE, "Error in sendToPlayerUpdatePage "+classMethod,e);
				e.printStackTrace();
			}
			catch(Exception ex)
			{
				LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
			}
		}

		HttpPostConnector c = null;
		try {
			c = new HttpPostConnector(new URL(model.Settings.UPDATE_CHECKER_URL), HttpPostConnector.getBoxToken()
					+ sb.toString());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			LoggingUtils.log(Level.SEVERE, "Error in sendToPlayerUpdatePage "+classMethod,e);
			e.printStackTrace();
		}
		catch(Exception ex)
		{
			LoggingUtils.log(Level.SEVERE, "Error in postUpdateComplete "+classMethod,ex);
		}
		c.post();
	}

	public static void main(String[] args) {
		
	}
}
